package com.hts.redis.redisson;


import com.hts.utils.string.StringFormat;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 洪松
 */
@Configuration
@Slf4j
public class RedissonConfig {
    @Bean
    public RedissonClient redissonClient(RedisProperties redisProperties) {
        log.info("初始redisson");
        Config config = new Config();
        config.setCodec(StringCodec.INSTANCE);
        config.setTransportMode(TransportMode.NIO);
        //单机服务信息
        String address = StringFormat.formatCurlyBraces("redis://{}:{}", redisProperties.getHost(), redisProperties.getPort());
        log.info("请求地址：{}", address);
        config.useSingleServer()
                .setAddress(address)
                .setDatabase(redisProperties.getDatabase())
                .setPassword(redisProperties.getPassword())
        ;
        log.info("初始redisson 完成");
        return Redisson.create(config);
    }

    @Bean
    public RedissonTemplate redissonTemplate(RedissonClient redissonClient) {
       return new RedissonTemplate(redissonClient);
    }
}
